package ivan.cheng.tuqu.activity;

import ivan.cheng.tuqu.Globals;
import ivan.cheng.tuqu.R;
import ivan.cheng.tuqu.adapter.GridDecratorAdapter;
import ivan.cheng.tuqu.adapter.GridFrameAdapter;
import ivan.cheng.tuqu.adapter.GridLomoAdapter;
import ivan.cheng.tuqu.adapter.GridScrawlAdapter;
import ivan.cheng.tuqu.graphics.ImageFilter;
import ivan.cheng.tuqu.graphics.JNI;
import ivan.cheng.tuqu.graphics.SpecialEffect;
import ivan.cheng.tuqu.util.BitmapUtil;
import ivan.cheng.tuqu.util.FileUtil;
import ivan.cheng.tuqu.util.LogUtil;
import ivan.cheng.tuqu.view.ColorPickerDialog;
import ivan.cheng.tuqu.view.ScrawlView;

import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class EffectsProcessingActivity extends UIActivity implements
		OnClickListener, ColorPickerDialog.OnColorChangedListener {
	private static final int REQUESTCODE_PUBLISH = 1;
	private static final int MAX_STORE_BITMAP = 2;
	private Button mReturnPhotogrBtn;
	private Button mShareAndPublishBtn;
	private ImageView mUndoIV;
	private ImageView mRedoIV;

	private ImageView mPhotoIV;

	private Button mEP1;//特效处理
	private Button mEP2;//拼图相框
	private Button mEP3;//装饰恶搞
	private Button mEP4;//文字泡泡
	private Button mEP5;//原图

	private ImageView mCancelHandle;
	private TextView mTVTitle;
	private ImageView mConfirmhandle;
	private ScrawlView mScrawlView;
	private ImageView mFrameView;// 相框
	private ImageView mIVRotate;
	private RelativeLayout mRLedit;
	private int mIid = 1000;
	private Gallery mToolGV;
	private boolean mbOutofMem = false;//是否在处理过程中出现内存不足了?
	private Bitmap mTempBmp;// 临时用来存储图片处理效果的
	private List<Bitmap> mBitmapLst;
	private List<WeakReference<Bitmap>> mBottomLst;// 图像处理底部的图片列表
	private int miBeautyPos;// 当前正在处理的图片的位置

	enum BeautyType {
		DECRATOR(0x01), TEXT(0x02), FRAME(0x03), SCRAML(0x04), LOMO(0x05);

		private int iType;

		private BeautyType(int i) {
			this.iType = i;
		}

		public int getiType() {
			return this.iType;
		}
	}//

//	enum LOMOType {
//		FILLLIGHT, SOFTGLOW, CHANGEHUE, CHANGESTUDIO_GREEN, CHANGESTUDIO_BLUE, CHANGESTUDIO_RETRO, CHANGESTUDIO_GRAY, CHANGESTUDIO_TOTAL, CHANGESTUDIO_FILM, CHANGESTUDIO_YELLOW, CHANGESTUDIO_CHANGEGREY,
//	}

	private BeautyType mBt;
	private BaseAdapter mBaseAdapter;
	// private GridLomoAdapter mGridLomoAdapter;
	// private GridDecratorAdapter mGridDecratorAdapter;
	// private GridTextPopAdapter mGridTextPopAdapter;
	// private GridScrawlAdapter mGridScrawlAdapter;
	// private GridFrameAdapter mGridFrameAdapter;

	private int[] pixels;// = new int[width * height];
	private int[] pixels2;//For combo bitmap
	short[] inBf;// = RGB_8888_To_RGB_565(pixels);
	short[] outBf;// = new short[inBf.length];
	private static final int MESSAGE_EFFECT_OVER = 0;
	private ProgressDialog mProgressdialog;
	private Handler mHandle = new EffectHandle();
	private boolean mbFromCamera;
	private Bitmap mTempRecycled;//用于保存可回收的bitmap
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.effects_processing);

		mReturnPhotogrBtn = (Button) this
				.findViewById(R.id.btn_return_photograph);
		mReturnPhotogrBtn.setOnClickListener(this);
		mShareAndPublishBtn = (Button) this.findViewById(R.id.btn_ok);
		mShareAndPublishBtn.setOnClickListener(this);
		mUndoIV = (ImageView) this.findViewById(R.id.btn_undo);
		mRedoIV = (ImageView) this.findViewById(R.id.btn_redo);
		mPhotoIV = (ImageView) this.findViewById(R.id.photo);

		mEP1 = (Button) this.findViewById(R.id.effective_btn);
		mEP1.setOnClickListener(this);
		mEP2 = (Button) this.findViewById(R.id.frame_combo_btn);
		mEP2.setOnClickListener(this);
		mEP3 = (Button) this.findViewById(R.id.decorator_btn);
		mEP3.setOnClickListener(this);
		mEP4 = (Button) this.findViewById(R.id.text_decorator_btn);
		mEP4.setOnClickListener(this);
		mEP5 = (Button) this.findViewById(R.id.origin_btn);
		mEP5.setOnClickListener(this);
		mUndoIV = (ImageView) this.findViewById(R.id.btn_undo);
		mUndoIV.setOnClickListener(this);
		mRedoIV = (ImageView) this.findViewById(R.id.btn_redo);
		mRedoIV.setOnClickListener(this);
		mTVTitle = (TextView) this.findViewById(R.id.tv_title);

		mCancelHandle = (ImageView) this.findViewById(R.id.IV_cancel);
		mCancelHandle.setOnClickListener(this);
		mConfirmhandle = (ImageView) this.findViewById(R.id.IV_ok);
		mConfirmhandle.setOnClickListener(this);

		mRLedit = (RelativeLayout) findViewById(R.id.RL_edit);

		mToolGV = (Gallery) findViewById(R.id.GV_tools);
		mToolGV.setOnItemClickListener(onToolSelectorClick);
		mToolGV.setOnItemSelectedListener(onToolSelectorItemSelected);
		
		
		mIVRotate = (ImageView) findViewById(R.id.IV_rotatetool);
		mRedoIV.setEnabled(false);
		mUndoIV.setEnabled(false);
		mIVRotate.setOnClickListener(this);// 为了能获取到onkeyup信息
		mIVRotate.setOnTouchListener(RotateScaleTool);

		mBottomLst = new ArrayList<WeakReference<Bitmap>>();
		mBitmapLst = new ArrayList<Bitmap>();
		mBaseAdapter = new GridLomoAdapter(this, mBottomLst);
		mToolGV.setAdapter(mBaseAdapter);
		mBt = BeautyType.LOMO;
		mEP1.setEnabled(false);
		//mToolGV.setSelection(1);
		mTVTitle.setText(R.string.effective_color_photo);
		Bitmap bitmap = (Bitmap) getIntent().getExtras().getParcelable(
				Globals.EXTRA_OBJECT);
		mbFromCamera = getIntent().getExtras().getBoolean(Globals.EXTRA_IMAGE_SOURCE);
		if (null == bitmap) {// 传的是路径
			String path = (String) getIntent().getExtras().getString(
					Globals.EXTRA_PHOTO_PATH);
			//这里不再对图像大小做处理
			bitmap = BitmapUtil.instance().DecodeFromFile(path);

		}
		//这里对mPhotoIV做一下简单调整
		if(bitmap != null && bitmap.getWidth() > getWindowManager().getDefaultDisplay().getWidth())
		{
			//LayoutParams lp = (LayoutParams) mPhotoIV.getLayoutParams();
			mPhotoIV.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth(),
					(int)(bitmap.getHeight() * (float)getWindowManager().getDefaultDisplay().getWidth()/bitmap.getWidth())));
		}
		mPhotoIV.setImageBitmap(bitmap);
		SaveBitmapToLst(bitmap);
		isThemeAvailable();

//		setMenuTopItemEnable(false);
//		setMenuUpdateItemEnable(false);
	}

	private void enableAllBtn()
	{
		mEP1.setEnabled(true);
		mEP2.setEnabled(true);
		mEP3.setEnabled(true);
		mEP4.setEnabled(true);
		mEP5.setEnabled(true);
	}
	
	private void SaveBitmapToLst(Bitmap bmp)
	{
		if(mBitmapLst.size() > MAX_STORE_BITMAP)
		{//超过数额了,删除掉一张,原图不做删除
			mBitmapLst.get(1).recycle();
			mBitmapLst.remove(1);
		}
		mBitmapLst.add(bmp);
		miBeautyPos = mBitmapLst.size() - 1;
	}
	
	private View.OnTouchListener RotateScaleTool = new View.OnTouchListener() {

		int[] temp = new int[4];
		float[] fsave = new float[2];

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			ImageView opIV = (ImageView) mIVRotate.getTag();
			if (opIV == null)// 没有操作对象
				return false;
			int eventaction = event.getAction();

			switch (eventaction) {

			case MotionEvent.ACTION_DOWN: // touch down so check if the

			{
				temp[0] = opIV.getLeft() + opIV.getWidth() / 2;
				temp[1] = opIV.getTop() + opIV.getHeight() / 2;
				temp[2] = (int) (v.getLeft() + event.getX());
				temp[3] = (int) (v.getTop() + event.getY());
				v.setVisibility(View.INVISIBLE);
				
			}

				break;

			case MotionEvent.ACTION_MOVE: // touch drag with the ball
			{
				int x0 = (int) (v.getLeft() + event.getX());
				int y0 = (int) (v.getTop() + event.getY());

				float gap = getGap(temp[0], temp[2], temp[1], temp[3]);
				float gap1 = getGap(temp[0], x0, temp[1], y0);
				float dgap = gap1 / gap;

				LogUtil.w("temp[0]:" + temp[0] + "temp[1]:" + temp[1]
						+ "temp[2]:" + temp[2] + "temp[3]:" + temp[3] + "x0:"
						+ x0 + "y0:" + y0);
				LogUtil.d("gap:" + gap + "gap1:" + gap1 + "dgap:" + dgap);

				double r = Math.atan2(temp[2] - temp[0], temp[3] - temp[1]);
				double dRotation = Math.toDegrees(r);
				r = Math.atan2(x0 - temp[0], y0 - temp[1]);
				double dRotation1 = Math.toDegrees(r);
				float newRot = new Float(dRotation - dRotation1);
				Float degrees = (Float) opIV.getTag(R.id.image_matrix_angle);
				if (null == degrees)
					degrees = new Float(0.0);
				Float fScale = (Float) opIV.getTag(R.id.image_matrix_scale);
				if (null == fScale)
					fScale = new Float(1.0);
				dgap *= fScale;
				Matrix matrix = new Matrix();
				degrees += newRot;
				degrees = degrees % 360;

				Bitmap bmp = (Bitmap) opIV.getTag(R.id.image_object);
				matrix.postRotate(degrees, bmp.getWidth() / 2,
						bmp.getHeight() / 2);
				matrix.postScale(dgap, dgap, bmp.getWidth() / 2, bmp
						.getHeight() / 2);

				fsave[0] = degrees;
				fsave[1] = dgap;
				Bitmap bm = BitmapUtil.instance().createBitmap(bmp, 0, 0,
						bmp.getWidth(), bmp.getHeight(), matrix, true);

				//int opIVWidth = opIV.getWidth();
				//int opIVHeight = opIV.getHeight();
				//opIV.offsetLeftAndRight(-(bm.getWidth() - opIVWidth)/2);
				//opIV.offsetTopAndBottom(-(bm.getHeight() - opIVHeight)/2);
				opIV.setImageBitmap(bm);
				//opIV.postInvalidate();
			}

				break;

			case MotionEvent.ACTION_UP: {

				opIV.setTag(R.id.image_matrix_angle, fsave[0]);
				opIV.setTag(R.id.image_matrix_scale, fsave[1]);
				setRatateToolPosition(opIV);
			}

				break;
			}

			return false;
		}
	};

	private float getGap(float x0, float x1, float y0, float y1) {
		return (float) Math.pow(
				Math.pow((x0 - x1), 2) + Math.pow((y0 - y1), 2), 0.5);
	}

	private void isThemeAvailable() {
		// 首先去获取
//		String sOldTheme = Setting.getOldThemeSharedPreferences(this);
//		String sNewTheme = Setting.getNewThemeSharedPreferences(this);
//		if (sNewTheme != null && !sNewTheme.equals(sOldTheme)) {
//			// 有新主题需要下载
//			// 先转移下特效
//			ThemeSpecDownLoadManager.getInstance(mApp).CopyThemeToSpecFile();
//			Setting.setOldThemeSharedPreferences(this, sNewTheme);
//			showDialog(THEMENOTIFY);
//		}
	}

	private OnItemClickListener onToolSelectorClick = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (mBt) {
			case LOMO:
				OnLomo(view, position);
				mConfirmhandle.setEnabled(true);
				break;
			case DECRATOR:
				AddDecorator(view, position, BeautyType.DECRATOR);
				mConfirmhandle.setEnabled(true);
				break;
			case FRAME:
				AddFrame(view, position);
				mConfirmhandle.setEnabled(true);
				break;
			case TEXT:
				AddDecorator(view, position, BeautyType.TEXT);
				mConfirmhandle.setEnabled(true);
				break;
			case SCRAML:
				ScrawlToolSelect(view, position);
				mConfirmhandle.setEnabled(true);
				break;
			}

		}
	};

	private OnItemSelectedListener onToolSelectorItemSelected = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			// Toast.makeText(ChatActivity.this,
			// ChatEmotion.EMO_STRINGS[position], Toast.LENGTH_SHORT).show();

		}

		public void onNothingSelected(AdapterView<?> parent) {

		}
	};

	@Override
	public void onClick(View v) {
		int iID = v.getId();
		switch (iID) {
		case R.id.IV_ok:// 图片处理的ok按钮
			OnConfirm();
			break;
		case R.id.IV_cancel:// 图片处理的取消按钮
			Oncancel();
			break;
		case R.id.btn_ok:
			SaveAndPublish();
			break;
		case R.id.btn_return_photograph: {
			// Intent intent = new Intent(this, PhotographActivity.class);
			// Bundle myPhotographbl = new Bundle();
			// myPhotographbl.putString(Globals.EXTRA_PHOTOGRAPH_CATEGORY,
			// PhotographActivity.PHOTOGRAPHCATE_PUBLISH_PHOTO);
			// intent.putExtras(myPhotographbl);
			// startActivity(intent);
			finish();
		}

			break;
		case R.id.effective_btn: 
		{
			OnConfirm();
			enableAllBtn();
			mEP1.setEnabled(false);
			mTVTitle.setText(R.string.effective_color_photo);
			mBt = BeautyType.LOMO;
			
			clearBottomLst();
			
			mBaseAdapter = new GridLomoAdapter(this, mBottomLst);
			mToolGV.setAdapter(mBaseAdapter);
			//mToolGV.setSelection(1);
			//Animation an = AnimationUtils.loadAnimation(this,
			//		R.anim.translate_gallery);
			//mToolGV.startAnimation(an);
			//mToolGV.setVisibility(View.VISIBLE);
			//findViewById(R.id.bottom_tab_bar).setVisibility(View.GONE);
			//findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
			//findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);
			//mConfirmhandle.setEnabled(false);
			mBaseAdapter.notifyDataSetChanged();
		}
			break;
		case R.id.decorator_btn: {
			OnConfirm();
			enableAllBtn();
			mEP3.setEnabled(false);
			mBt = BeautyType.DECRATOR;
			mTVTitle.setText(R.string.effective_decorator_photo);
			clearBottomLst();
			mBaseAdapter = new GridDecratorAdapter(this, mApp, mBottomLst);
			mToolGV.setAdapter(mBaseAdapter);
			//mToolGV.setSelection(1);
//			Animation an = AnimationUtils.loadAnimation(this,
//					R.anim.translate_gallery);
//			mToolGV.startAnimation(an);
//			mToolGV.setVisibility(View.VISIBLE);
//			findViewById(R.id.bottom_tab_bar).setVisibility(View.GONE);
//			findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
//			findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);
//			mConfirmhandle.setEnabled(false);
			mBaseAdapter.notifyDataSetChanged();
		}
			break;
//		case R.id.text_decorator_btn: 
//		{
//			OnConfirm();
//			enableAllBtn();
//			mEP4.setEnabled(false);
//			mTVTitle.setText(R.string.effective_textpop_photo);
//			mBt = BeautyType.TEXT;
//			clearBottomLst();
//			mBaseAdapter = new GridTextPopAdapter(this, mApp, mBottomLst);
//			mToolGV.setAdapter(mBaseAdapter);
//			//mToolGV.setSelection(1);
////			Animation an = AnimationUtils.loadAnimation(this,
////					R.anim.translate_gallery);
////			mToolGV.startAnimation(an);
////			mToolGV.setVisibility(View.VISIBLE);
////			findViewById(R.id.bottom_tab_bar).setVisibility(View.GONE);
////			findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
////			findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);
////			mConfirmhandle.setEnabled(false);
//			mBaseAdapter.notifyDataSetChanged();
//		}
//			break;
		case R.id.text_decorator_btn: 
		{
			OnConfirm();
			enableAllBtn();
			mEP4.setEnabled(false);
			mBt = BeautyType.SCRAML;
			mTVTitle.setText(R.string.effective_optimize_photo);
			
			int iWidth = mBitmapLst.get(miBeautyPos).getWidth();
			int iHeight = mBitmapLst.get(miBeautyPos).getHeight();
			if(null == mScrawlView)
				mScrawlView = new ScrawlView(this);
			if(false == mScrawlView.init(iWidth, iHeight))
			{
				Toast.makeText(this,getString(R.string.out_of_memory), Toast.LENGTH_SHORT).show();
				break;
			}
			if (!mRLedit.equals(mScrawlView.getParent())) 
			{
				LayoutParams  layout = (LayoutParams)mPhotoIV.getLayoutParams();
				layout.width = iWidth;
				layout.height = iHeight;
				mRLedit.addView(mScrawlView, layout);
			}
			clearBottomLst();
			mBaseAdapter = new GridScrawlAdapter(this, mBottomLst);
			mToolGV.setAdapter(mBaseAdapter);
			
//			Animation an = AnimationUtils.loadAnimation(this,
//					R.anim.translate_gallery);
//			mToolGV.startAnimation(an);
//			mToolGV.setVisibility(View.VISIBLE);
//			findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
//			findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);
//			//findViewById(R.id.bottom_tab_bar).setVisibility(View.GONE);
//			mConfirmhandle.setEnabled(true);
			mBaseAdapter.notifyDataSetChanged();
		}
			break;
		case R.id.frame_combo_btn: {
			OnConfirm();
			enableAllBtn();
			mEP2.setEnabled(false);
			mBt = BeautyType.FRAME;
			mTVTitle.setText(R.string.effective_frame_photo);
			clearBottomLst();
			mBaseAdapter = new GridFrameAdapter(this, mApp, mBottomLst);
			mToolGV.setAdapter(mBaseAdapter);
			//mToolGV.setSelection(1);
//			Animation an = AnimationUtils.loadAnimation(this,
//					R.anim.translate_gallery);
//			mToolGV.startAnimation(an);
//			mToolGV.setVisibility(View.VISIBLE);
//			findViewById(R.id.bottom_tab_bar).setVisibility(View.GONE);
//			findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
//			findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);
//			mConfirmhandle.setEnabled(false);
			mBaseAdapter.notifyDataSetChanged();
		}
			break;
		case R.id.origin_btn:
			BackToOriginalPhoto();
			break;
		case R.id.btn_undo:
			OnUndo();
			break;
		case R.id.btn_redo:
			OnRedo();
			break;
		default:
			break;
		}
	}

	private void BackToOriginalPhoto() 
	{
		for (int i = 0; i < mBitmapLst.size(); i++) {
			if(0 == i)
				continue;
			if (null != mBitmapLst.get(i) && !mBitmapLst.get(i).isRecycled())
				mBitmapLst.get(i).recycle();
		}
		miBeautyPos = 0;
		mPhotoIV.setImageBitmap(mBitmapLst.get(miBeautyPos));
		if(mTempBmp != null && !mTempBmp.isRecycled())
		{
			mTempBmp.recycle();
			mTempBmp = null;
		}
		if(null != mFrameView)
		{
			Bitmap bmp = (Bitmap) mFrameView.getTag(R.id.image_object);
			if(null != bmp && !bmp.isRecycled())
			{
				mFrameView.setTag(R.id.image_object,null);
				bmp.recycle();
			}
			mFrameView.setImageBitmap(null);
		}
		RemoveAllDecrator();
	}

	private void clearBottomLst() 
	{
		for (int i = 0; i < mBottomLst.size(); i++) {
			if (null != mBottomLst.get(i)
					&& null!= mBottomLst.get(i).get()
					&& !mBottomLst.get(i).get().isRecycled())
				mBottomLst.get(i).get().recycle();
		}
		mBottomLst.clear();
	}

//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK) {
//			if (mToolGV.isShown()) {
//				Oncancel();
//				return true;
//			}
//			finish();
//		}
//		return super.onKeyDown(keyCode, event);
//	}

	protected void ScrawlToolSelect(View view, int position) {

		mScrawlView.getPaint().setXfermode(null);
		mScrawlView.getPaint().setAlpha(0xFF);
		mScrawlView.setType(position);
		switch (position) {
		case 0:// 立体线条
			mScrawlView.getPaint().setMaskFilter(mScrawlView.getEmboss());
			break;
		case 1:// 柔和线条
			mScrawlView.getPaint().setMaskFilter(mScrawlView.getBlur());
			break;
		case 2:// 实体线条线条
			mScrawlView.getPaint().setMaskFilter(null);
			break;
		case 3:// 橡皮擦
			mScrawlView.getPaint().setXfermode(
					new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
			break;
		case 4:// 颜色选择
			new ColorPickerDialog(this, this, mScrawlView.getPaint().getColor())
					.show();
			break;
		case 5:// 笔划粗细
			break;
		case /* SRCATOP_MENU_ID */6:
			mScrawlView.getPaint().setXfermode(
					new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
			mScrawlView.getPaint().setAlpha(0x80);
		default:
			break;
		}
	}

	protected void AddFrame(View view, int position) {
		// 获取当前需要显示的相框的图片
		// 获取当前需要显示的装饰品的图片
		Bitmap bmp = null;
		try {
			String sPath = (String) view.getTag(R.id.image_file_path);
			if(null != sPath)
			{
				bmp = BitmapUtil.instance().DecodeFromFile(sPath);
			}
			else if(null != (sPath = (String) view.getTag(R.id.image_assert_path)))
			{
				bmp = BitmapUtil.instance().DecodeFromStream(this.getAssets().open(sPath));
			}
			else
			{
				//return;
			}
				
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		if (null != view.getTag(R.id.image_download_more)) {// 说明是下载更多按钮
			DownLoadMore(BeautyType.FRAME);
			return;
		}

		//findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
		//findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);

		if (null == mFrameView) {
			mFrameView = new ImageView(this);
		}
		else
		{
			Bitmap bitmap = (Bitmap) mFrameView.getTag(R.id.image_object);
			if(null != bitmap && !bitmap.isRecycled())
			{
				bitmap.recycle();
			}
		}
		if (!mRLedit.equals(mFrameView.getParent())) 
		{
			mRLedit.addView(mFrameView,mPhotoIV.getLayoutParams());
		}
		bmp = GetSuitFrame(bmp);
		mFrameView.setTag(R.id.image_object, bmp);
		mFrameView.setImageBitmap(bmp);
	}

	private void OnRedo() {

		miBeautyPos++;
		if (miBeautyPos >= mBitmapLst.size() - 1) {
			mRedoIV.setEnabled(false);
			miBeautyPos = mBitmapLst.size() - 1;
		}
		mUndoIV.setEnabled(true);
		mPhotoIV.setImageBitmap(mBitmapLst.get(miBeautyPos));
	}

	private void OnUndo() 
	{
		miBeautyPos--;
		if (miBeautyPos <= 0) {
			miBeautyPos = 0;
			mUndoIV.setEnabled(false);
		}
		mRedoIV.setEnabled(true);
		mPhotoIV.setImageBitmap(mBitmapLst.get(miBeautyPos));
	}

	protected void OnLomo(View view, final int position) {
		//findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
		//findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);
		
		mProgressdialog = ProgressDialog.show(this,
				getString(R.string.progressdialog_title),
				getString(R.string.wait_a_moment), false, false);
		new Thread(new Runnable() {
			
			@Override
			public void run() 
			{
				if(null == mTempBmp)
					mTempBmp = Bitmap
						.createBitmap(mBitmapLst.get(miBeautyPos).getWidth(),
								mBitmapLst.get(miBeautyPos).getHeight(), Bitmap.Config.ARGB_8888);
				mbOutofMem = false;
				switch(position)
				{
				case 0://提亮
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_danya);
					//Old();
					//OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,"frame06.png");
					break;
				case 1:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_gete);
					//Lomo();
					//OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,"frame07.png");
					break;
				case 2:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_guangyun);
					//Elegant();
					//Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_FUGU);
					break;
				case 3:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_heibai);
					//LomoHaolo();
					//OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_HEIBAI,"frame08.png");
					break;
				case 4:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_jiuhong);
					//Cinnamon();
					//OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_QIUSE,"frame09.png");
					break;
				case 5:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_landiao);
					//Reverse();
					//Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_SUMIAO);
					break;
				case 6:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_lomo);
					//BaoColor();
					//OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_FENHONG,"frame10.png");
					break;
				case 7:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_menghuan);
					//LomoC();
					//OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_LAOZHAOPIAN,"frame11.png");
					break;
				case 8:
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_qingning);
					//LomoHDR();
					//Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_ABAO);
					break;
				case 9:
					
					Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_TILIANG,ImageFilter.colormatrix_yese);
					break;
				case 10:
					XiaoQinxin();
					break;
				case 11:
					Retro();
					break;
				case 12:
					Japanese();
					break;
				case 13:
					Impression();
					break;
				case 14:
					//Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_HEIBAI);
					//Film();
					break;
				case 15:
					Sharp();
					break;
				case 16:
					LomoB();
				//	Effective(SpecialEffect.E_COMBO_SPEC_EFFECT.COMBO_SPEC_EFFECT_SUMIAO);
					break;
				default:
					break;
				}
				LogUtil.d("Effect position:"+position);
				mHandle.sendEmptyMessage(MESSAGE_EFFECT_OVER);
			}
			
			
		}).start();
	}

	@SuppressWarnings("unused")
	private Bitmap OnlomoCombo(SpecialEffect.E_COMBO_SPEC_EFFECT se,String strFrame) 
	{
		//Effective(se);
		if(null == mTempBmp)
			return null;
		int photoWidth = mPhotoIV.getWidth();
		int photoHeight = mPhotoIV.getHeight();
		Bitmap newb = BitmapUtil.instance().createBitmap(photoWidth,
				photoHeight, Config.RGB_565);// 创建一个新的和SRC长度宽度一样的位图
		if(null == newb)
		{
			Toast.makeText(this,getString(R.string.out_of_memory), Toast.LENGTH_SHORT).show();
			return newb;
		}
			
		Canvas cv = new Canvas(newb);
		cv.drawBitmap(mTempBmp, new Rect(0, 0, mTempBmp.getWidth(), mTempBmp
				.getHeight()), new Rect(0, 0, photoWidth, photoHeight), null);// 在
																				// 0，0坐标开始画入src

		Bitmap childBitmap;
		try {
			childBitmap = BitmapUtil.instance().DecodeFromStream(getAssets().open("framecombo/" + strFrame));
		} catch (IOException e) {
			return mTempBmp;
		}

		cv.drawBitmap(childBitmap, null,
				new Rect(0, 0, photoWidth, photoHeight), null);

		// save all clip
		cv.save(Canvas.ALL_SAVE_FLAG);// 保存

		// store
		cv.restore();// 存储
		mTempBmp.recycle();
		mTempBmp = newb;
		return mTempBmp;
	}

	private void Oncancel() {
		switch (mBt) {
		case SCRAML:
			{
				mRLedit.removeView(mScrawlView);
				if(null != mScrawlView.GetBitmap() && !mScrawlView.GetBitmap().isRecycled())
					mScrawlView.GetBitmap().recycle();
			}
			break;
		case LOMO:
			mPhotoIV.setImageBitmap(mBitmapLst.get(miBeautyPos));
			break;
		case TEXT:
		case DECRATOR://
			RemoveAllDecrator();
			break;
		case FRAME:
			{
				if(null != mFrameView)
				{
					Bitmap bitmap = (Bitmap) mFrameView.getTag(R.id.image_object);
					if(null != bitmap && !bitmap.isRecycled())
					{
						bitmap.recycle();
					}
					mRLedit.removeView(mFrameView);
				}
			}
			
			break;
		}
		findViewById(R.id.LL_shareBar).setVisibility(View.VISIBLE);
		findViewById(R.id.LL_editBar).setVisibility(View.GONE);
		mToolGV.setVisibility(View.GONE);

		Animation an = AnimationUtils.loadAnimation(this,
				R.anim.translate_gallery);
//		findViewById(R.id.bottom_tab_bar).startAnimation(an);
//		findViewById(R.id.bottom_tab_bar).setVisibility(View.VISIBLE);
//		findViewById(R.id.bottom_tab_bar).setVisibility(View.VISIBLE);

	}

	@Override
	protected void RegisterEvent() {
		// 注册本模块需要关注的广播事件
		IntentFilter i = new IntentFilter();
//		i.addAction(I_BROADCAST_EVENT.sFINISH);
//		i.addAction(I_BROADCAST_EVENT.sFINISHMYSELF);
//		i.addAction(I_BROADCAST_EVENT.sOOM);
//		i.addAction(I_BROADCAST_EVENT.sEFFECTIVE_IMAGE_DOWNLOADED);
		registerReceiver(mMessageReceiver, i);
	}

	@Override
	protected void OnBroadCastReceive(Context context, Intent intent) {
		super.OnBroadCastReceive(context, intent);
		String action = intent.getAction();
//		if (action.equals(I_BROADCAST_EVENT.sEFFECTIVE_IMAGE_DOWNLOADED)) 
//		{
//			if(mBaseAdapter != null)
//			{
//				clearBottomLst();
//				mBaseAdapter.notifyDataSetChanged();
//			}
//		}
	}
	
	private void OnConfirm() {
		// 把图片保存到列表
		if(null == mBt)
			return;
		Bitmap tempBmp = null;
		switch (mBt) {
		case SCRAML:
			{
				tempBmp = SaveScrawl();
				mRLedit.removeView(mScrawlView);
				if(null == tempBmp)
				{
					Toast.makeText(this,getString(R.string.out_of_memory), Toast.LENGTH_SHORT).show();
					return;
				}
			}
			break;
		case LOMO:
			if(null == mTempBmp)
				return;
			tempBmp = SaveLomo();
			break;
		case DECRATOR://
		case TEXT:
			{
				if(mRLedit.getChildCount() <= 2)
					return;
				tempBmp = SaveDecrator();
				RemoveAllDecrator();
				if(null == tempBmp)
				{
					Toast.makeText(this,getString(R.string.out_of_memory), Toast.LENGTH_SHORT).show();
					return;
				}
			}
			break;
		case FRAME:
			{
				if(null == mFrameView)
					return;
				tempBmp = SaveFrame();
				mRLedit.removeView(mFrameView);
				if(null == tempBmp)
				{
					Toast.makeText(this,getString(R.string.out_of_memory), Toast.LENGTH_SHORT).show();
					return;
				}
			}
			break;
		}
		SaveBitmapToLst(tempBmp);
		mUndoIV.setEnabled(true);
		mPhotoIV.setImageBitmap(tempBmp);
		mTempBmp = null;
		//findViewById(R.id.LL_shareBar).setVisibility(View.VISIBLE);
		//findViewById(R.id.LL_editBar).setVisibility(View.GONE);
		//mToolGV.setVisibility(View.GONE);
		//findViewById(R.id.bottom_tab_bar).setVisibility(View.VISIBLE);
	}

	private Bitmap SaveLomo() {

		return mTempBmp;
	}

	/**生成一个和图片相应的相框*/
	private Bitmap GetSuitFrame(Bitmap bmpFrame)
	{
		Bitmap photoBG = mBitmapLst.get(miBeautyPos);// ((BitmapDrawable)mPhotoIV.getBackground()).getBitmap();
		int photoWidth = photoBG.getWidth();
		int photoHeight = photoBG.getHeight();
		Bitmap newb = BitmapUtil.instance().createBitmap(photoWidth,
				photoHeight, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
		if(null == newb)
			return newb;
		Canvas cv = new Canvas(newb);
		if(null != bmpFrame)
		{
			mFrameView.setTag(R.id.image_object,null);
			cv.drawBitmap(bmpFrame, null,
					new Rect(0, 0, photoWidth, photoHeight), null);
			bmpFrame.recycle();
		}
		// save all clip
		cv.save(Canvas.ALL_SAVE_FLAG);// 保存

		// store
		cv.restore();// 存储
		return newb;
	}
	
	private Bitmap SaveFrame() 
	{
		Bitmap photoBG = mBitmapLst.get(miBeautyPos);// ((BitmapDrawable)mPhotoIV.getBackground()).getBitmap();
		int photoWidth = photoBG.getWidth();
		int photoHeight = photoBG.getHeight();
		Bitmap newb = BitmapUtil.instance().createBitmap(photoWidth,
				photoHeight, Config.RGB_565);// 创建一个新的和SRC长度宽度一样的位图
		if(null == newb)
			return newb;
		Canvas cv = new Canvas(newb);
		cv.drawBitmap(photoBG, new Rect(0, 0, photoBG.getWidth(), photoBG
				.getHeight()), new Rect(0, 0, photoWidth, photoHeight), null);// 在
																				// 0，0坐标开始画入src

		Bitmap childBitmap = (Bitmap) mFrameView.getTag(R.id.image_object);
		if(null != childBitmap)
		{
			mFrameView.setTag(R.id.image_object,null);
			cv.drawBitmap(childBitmap, null,
					new Rect(0, 0, photoWidth, photoHeight), null);
			childBitmap.recycle();
		}
		// save all clip
		cv.save(Canvas.ALL_SAVE_FLAG);// 保存

		// store
		cv.restore();// 存储
		return newb;
	}

	private void RemoveAllDecrator() {
		for (int i = mRLedit.getChildCount() - 1; i >= 0; i--) {
			ImageView childImageView = (ImageView) (mRLedit.getChildAt(i));
			if (childImageView.equals(mPhotoIV))
				continue;
			if (childImageView.equals(mIVRotate)) {
				mIVRotate.setVisibility(View.GONE);
				continue;
			}
			Bitmap bmp = (Bitmap) childImageView.getTag(R.id.image_object);
			if(null != bmp && !bmp.isRecycled())
				bmp.recycle();
			mRLedit.removeView(childImageView);
		}
	}

	private Bitmap SaveDecrator() {
		Bitmap photoBG = mBitmapLst.get(miBeautyPos);// ((BitmapDrawable)mPhotoIV.getBackground()).getBitmap();
		int photoWidth = photoBG.getWidth();
		int photoHeight = photoBG.getHeight();
		Bitmap newb = BitmapUtil.instance().createBitmap(photoWidth,
				photoHeight, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
		if(null == newb)
			return newb;
		Canvas cv = new Canvas(newb);
		cv.drawBitmap(photoBG, new Rect(0, 0, photoBG.getWidth(), photoBG
				.getHeight()), new Rect(0, 0, photoWidth, photoHeight), null);// 在
																				// 0，0坐标开始画入src

		for (int i = 1; i < mRLedit.getChildCount(); i++) {
			ImageView childImageView = (ImageView) (mRLedit.getChildAt(i));
			// draw icon into
			if (childImageView.equals(mIVRotate)
					|| childImageView.equals(mPhotoIV))
				continue;
			Bitmap childBitmap = (Bitmap) childImageView
					.getTag(R.id.image_object);
			childImageView.setTag(R.id.image_object,null);
			Matrix matrix = new Matrix();
			Float degrees = (Float) childImageView
					.getTag(R.id.image_matrix_angle);
			if (null != degrees)
				matrix.postRotate(degrees, childBitmap.getWidth() / 2,
						childBitmap.getHeight() / 2);
			Float fScale = (Float) childImageView
					.getTag(R.id.image_matrix_scale);
			if (null != fScale)
				matrix.postScale(fScale, fScale, childBitmap.getWidth() / 2,
						childBitmap.getHeight() / 2);
			matrix.postScale((float)photoHeight/mPhotoIV.getHeight(), (float)photoHeight/mPhotoIV.getHeight(), childBitmap.getWidth() / 2,
					childBitmap.getHeight() / 2);
			Bitmap bm = BitmapUtil.instance().createBitmap(childBitmap, 0, 0,
					childBitmap.getWidth(), childBitmap.getHeight(), matrix,
					true);
										/**不会太宽，只会太高*/
			int idx = (photoWidth - photoWidth * mPhotoIV.getHeight()/photoHeight)/2;
			int xLoc = childImageView.getLeft() - mPhotoIV.getLeft() - idx;
			int yLoc = childImageView.getTop() - mPhotoIV.getTop();
			xLoc = xLoc*photoHeight/mPhotoIV.getHeight();
			yLoc = yLoc*photoHeight/mPhotoIV.getHeight();
			
			cv.drawBitmap(bm, xLoc, yLoc, null);
			if (!childBitmap.equals(bm))
				bm.recycle();
			childBitmap.recycle();
		}
		// save all clip
		cv.save(Canvas.ALL_SAVE_FLAG);// 保存

		// store
		cv.restore();// 存储
		return newb;
	}

	private Bitmap SaveScrawl() {

		Bitmap photoBG = mBitmapLst.get(miBeautyPos);// ((BitmapDrawable)mPhotoIV.getBackground()).getBitmap();
		int photoWidth = photoBG.getWidth();
		int photoHeight = photoBG.getHeight();
		Bitmap newb = BitmapUtil.instance().createBitmap(photoWidth,
				photoHeight, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
		if(null == newb)
			return null;
		Canvas cv = new Canvas(newb);
		cv.drawBitmap(photoBG, new Rect(0, 0, photoBG.getWidth(), photoBG
				.getHeight()), new Rect(0, 0, photoWidth, photoHeight), null);// 在
																				// 0，0坐标开始画入src

		Bitmap childBitmap = mScrawlView.GetBitmap();
		if(null != childBitmap)
		{
			Matrix matrix = new Matrix();
			matrix.postScale((float)photoWidth/mPhotoIV.getWidth(), (float)photoHeight/mPhotoIV.getHeight(), childBitmap.getWidth() / 2,
					childBitmap.getHeight() / 2);
			Bitmap bm = BitmapUtil.instance().createBitmap(childBitmap, 0, 0,
					childBitmap.getWidth(), childBitmap.getHeight(), matrix,
					true);
			if(null != bm)
			{
				if(!bm.equals(childBitmap))
					childBitmap.recycle();
				cv.drawBitmap(bm, 0, 0, null);
				if(!bm.isRecycled())
					bm.recycle();
			}
		}
		
		// save all clip
		cv.save(Canvas.ALL_SAVE_FLAG);// 保存

		// store
		cv.restore();// 存储

		// mScrawlView = null;
		return newb;

	}

	protected void AddDecorator(View view, int position, BeautyType bt) {

		// 获取当前需要显示的装饰品的图片
		Bitmap bmp = null;
		try {
			String sPath = (String) view.getTag(R.id.image_file_path);
			if(null != sPath)
			{
				bmp = BitmapUtil.instance().DecodeFromFile(sPath);
			}
			else if(null != (sPath = (String) view.getTag(R.id.image_assert_path)))
			{
				bmp = BitmapUtil.instance().DecodeFromStream(this.getAssets().open(sPath));
			}
			else
			{
				//return;
			}
				
		} catch (IOException e) 
		{
			e.printStackTrace();
		}
		if (null != view.getTag(R.id.image_download_more)) {// 说明是下载更多按钮
			DownLoadMore(bt);
			return;
		}
		//findViewById(R.id.LL_shareBar).setVisibility(View.GONE);
		//findViewById(R.id.LL_editBar).setVisibility(View.VISIBLE);

		ImageView iv = new ImageView(EffectsProcessingActivity.this);
		iv.setId(++mIid);
		
		if(null != view.getTag(R.id.image_matrix_scale))
		{
			Matrix matrix = new Matrix();
			float fscale = (Float)view.getTag(R.id.image_matrix_scale);
			matrix.postScale(fscale, fscale, bmp.getWidth() / 2, bmp
					.getHeight() / 2);
			Bitmap bm = BitmapUtil.instance().createBitmap(bmp, 0, 0,
					bmp.getWidth(), bmp.getHeight(), matrix, true);
			//if(!bmp.equals(bm))
			//	bmp.recycle();
			//bmp = bm;
			//view.setTag(R.id.image_object,bm);
			iv.setImageBitmap(bm);
			iv.setTag(R.id.image_object, bm);
		}
		else
		{
			iv.setImageBitmap(bmp);
			iv.setTag(R.id.image_object, bmp);
		}
	
		
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.leftMargin = (mRLedit.getRight() - bmp.getWidth()) / 2;
		lp.topMargin = (mRLedit.getBottom() - bmp.getHeight()) / 2;
		mRLedit.addView(iv, lp);

		iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		iv.setOnTouchListener(new View.OnTouchListener() {

			int[] temp = new int[4];

			@Override
			public boolean onTouch(View v, MotionEvent event) {

				int eventaction = event.getAction();

				int x = (int) event.getRawX();
				int y = (int) event.getRawY();

				switch (eventaction) {

				case MotionEvent.ACTION_DOWN: // touch down so check if the
					temp[0] = (int) event.getX();
					temp[1] = y - v.getTop();
					mIVRotate.setVisibility(View.GONE);
					mRLedit.bringChildToFront(v);
					break;

				case MotionEvent.ACTION_MOVE: // touch drag with the ball
				{
					int left = x - temp[0];
					int right = x + v.getWidth() - temp[0];
					int top = y - temp[1];
					int bottom = y - temp[1] + v.getHeight();
					v.layout(left, top, right, bottom);
					LogUtil.i("left:" + left + "right:" + right + "top:" + top
							+ "bottom:" + bottom);

					v.postInvalidate();
				}

					break;

				case MotionEvent.ACTION_UP: {
					RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
							RelativeLayout.LayoutParams.WRAP_CONTENT,
							RelativeLayout.LayoutParams.WRAP_CONTENT);

					lp.leftMargin = x - temp[0];
					lp.topMargin = y - temp[1];
					int iRight = mRLedit.getRight()
							- (x - temp[0] + v.getWidth());
					int iBottom = mRLedit.getBottom()
							- (y - temp[1] + v.getHeight());
					if (iRight < 0) {
						lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT/*
																	 * |RelativeLayout
																	 * .
																	 * ALIGN_PARENT_BOTTOM
																	 */);
						lp.rightMargin = iRight;
					}
					if (iBottom < 0)
						lp.bottomMargin = iBottom;

					v.setLayoutParams(lp);

					LogUtil.i("v left:" + v.getLeft() + "right:" + v.getRight()
							+ "top:" + v.getTop() + "bottom:" + v.getBottom());
					if (v.getRight() <= mPhotoIV.getLeft()
							|| v.getLeft() >= mPhotoIV.getRight()
							|| v.getTop() >= mPhotoIV.getBottom()
							|| v.getBottom() <= mPhotoIV.getTop()) {
						mRLedit.removeView(v);
					} else {
						setRatateToolPosition((ImageView) v);
					}

				}
					break;
				}

				return false;
			}
		});
	}

	private void DownLoadMore(BeautyType bt) {
//		Intent intent = new Intent();
//		intent.setClass(this, EffectsDownloadActivity.class);
//		intent.putExtra(Globals.EXTRA_TYPE, bt.getiType());
//		startActivity(intent);
	}

	private void setRatateToolPosition(ImageView iv) {
		mIVRotate.setTag(iv);
		ImageView IVPhoto = (ImageView) findViewById(R.id.photo);
		mRLedit.bringChildToFront(mIVRotate);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);

		if (mIVRotate.getWidth() + iv.getRight() < IVPhoto.getRight()) {
			lp.leftMargin = iv.getRight();
		} else if (mIVRotate.getWidth() < iv.getLeft()) {
			lp.leftMargin = iv.getLeft() - mIVRotate.getWidth();
		} else {
			lp.leftMargin = IVPhoto.getRight() - mIVRotate.getWidth();
		}

		if (mIVRotate.getHeight() < iv.getTop()) {
			lp.topMargin = iv.getTop() - mIVRotate.getHeight();
		} else // if(mIVRotate.getHeight() + iv.getBottom() >
				// IVPhoto.getBottom())
		{
			lp.topMargin = iv.getBottom();
		}
		// else
		// {
		// lp.topMargin = IVPhoto.getBottom() - mIVRotate.getHeight();
		// }
		mIVRotate.setLayoutParams(lp);
		mIVRotate.setVisibility(View.VISIBLE);
	}

	private void SaveAndPublish() {

		// 先保存到本地 文件命名规则 用户ID+当前
		OnConfirm();
		long nlong = System.currentTimeMillis();
		String filePath = Globals.MX_FILE_PATH_DSIPOSE + nlong + ".jpg";
//		if (0 == miBeautyPos) {// 是第一张图,我们给他压小点上传
//			Bitmap bitmap = BitmapUtil.instance().resizePicture(
//					mBitmapLst.get(miBeautyPos),
//					Setting.GetBigImageDimesion(this), filePath);
//			if (!bitmap.equals(mBitmapLst.get(miBeautyPos)))
//				bitmap.recycle();
//		} else {
			FileUtil.getInstance().storeImageToFile(
					mBitmapLst.get(miBeautyPos), filePath,Bitmap.CompressFormat.JPEG,90);
//		}

		// 跳转页面，发布图片
		Intent publishPhotoIntent = new Intent(this, PublishPhotoActivity.class);
		Bundle publishPhotoBL = new Bundle();
		publishPhotoBL.putString(
				Globals.EXTRA_START_PUBLISH_PHOTO_ACTIVITY_CATE,
				Globals.START_PUBLISH_PHOTO_ACTIVITY_CATE_PUBLISH_PHOTO);
		publishPhotoBL.putBoolean(Globals.EXTRA_IMAGE_SOURCE, mbFromCamera);
		publishPhotoBL.putString(Globals.EXTRA_PHOTO_PATH, filePath);
		publishPhotoIntent.putExtras(publishPhotoBL);
		startActivityForResult(publishPhotoIntent, REQUESTCODE_PUBLISH);
		// finish();
	}

	private static short pack565(int r, int g, int b) {
		return (short) ((((int) (r) << 8) & 0xF800)
				| (((int) (g) << 3) & 0x7E0) | (((int) (b) >> 3)));
	}

	/** 颜色值相互转化 */
	private int RGB_565_to_int(short rgb565) {
		int r = ((rgb565 & 0xF800) >> 11); // r
		int g = ((rgb565 & 0x07E0) >> 5); // g
		int b = ((rgb565 & 0x001F)); // b

		// amplify the image
		r <<= 3;
		g <<= 2;
		b <<= 3;

		int idst = Color.rgb(r, g, b);

		return idst;
	}

	public short[] RGB_8888_To_RGB_565(int[] isrc, short[] rgb565) {
		for (int i = 0; i < isrc.length; i++) {
			int value = isrc[i];
			rgb565[i] = pack565(Color.red(value), Color.green(value), Color
					.blue(value));
		}

		return rgb565;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		for (int i = 0; i < mBitmapLst.size(); i++) {
			if (null != mBitmapLst.get(i) && !mBitmapLst.get(i).isRecycled())
				mBitmapLst.get(i).recycle();
		}
		mBitmapLst.clear();

		clearBottomLst();
		
		if (mTempBmp != null && mTempBmp.isRecycled())
			mTempBmp.recycle();
		if (mTempRecycled != null && mTempRecycled.isRecycled())
			mTempRecycled.recycle();
		RemoveAllDecrator();
		if(null != mFrameView)
		{
			Bitmap bitmap = (Bitmap) mFrameView.getTag(R.id.image_object);
			if(null != bitmap && !bitmap.isRecycled())
			{
				bitmap.recycle();
			}
		}
		if(null != mScrawlView)
		{
			Bitmap bitmap = mScrawlView.GetBitmap();
			if(null != bitmap && !bitmap.isRecycled())
			{
				bitmap.recycle();
			}
		}
		pixels = null;
		inBf = null;
		outBf = null;
		pixels2 = null;
	}

	public int[] RGB_565_To_RGB_8888(short[] rgb565, int[] rgb8888) {
		for (int i = 0; i < rgb565.length; i++) {
			// short rgb565 = (short)((bytes[i++] << 8) + (bytes[i] & 0xFF));
			rgb8888[i] = RGB_565_to_int(rgb565[i]);
		}

		return rgb8888;
	}

	class EffectHandle extends Handler
	{

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			switch (msg.what) {
			case MESSAGE_EFFECT_OVER:
				LogUtil.d("MESSAGE_EFFECT_OVER");
				if(null != mProgressdialog)
					mProgressdialog.dismiss();
				if(null != mTempBmp)
				{
					mPhotoIV.setImageBitmap(mTempBmp);
					if(true == mbOutofMem)
					{
						ShowOOMToast();
						BackToOriginalPhoto();
						LogUtil.e("BackToOriginalPhoto");
					}
				}

				if(mTempRecycled != null && !mTempRecycled.isRecycled() && !mTempRecycled.equals(mTempBmp))
					mTempRecycled.recycle();
				mTempRecycled = null;
				break;
			}
		}

		
	}
	
	private void Effective(SpecialEffect.E_COMBO_SPEC_EFFECT lt,float f[]) 
	{
		
		//首先获取图片像素数据
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
			inBf = new short[pixels.length];
			outBf = new short[inBf.length];
		}
		if(null == inBf || outBf == null)
		{//妙秀库里初始化了pixels的话就需要这个来初始化inBf,outBf
			inBf = new short[pixels.length];
			outBf = new short[inBf.length];
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);

		ImageFilter image = new ImageFilter(pixels, width, height);
		mTempRecycled = mTempBmp;
		//Bitmap tempBmp = mTempBmp;
		mTempBmp = image.iosFilter(f);
		//mPhotoIV.setImageBitmap(mTempBmp);
//		if (tempBmp != null && !tempBmp.isRecycled())
//			tempBmp.recycle();
//		tempBmp = null;
		if(true)
			return;
		
		
		
		RGB_8888_To_RGB_565(pixels,inBf);

		SpecialEffect se = new SpecialEffect();
		
		try
		{
			se.ComboSpecEffect(inBf, inBf.length, outBf, outBf.length, width, height,lt);
				
			RGB_565_To_RGB_8888(inBf,pixels);
			mTempRecycled = mTempBmp;
			mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
		}		
		catch(Exception e)
		{
			LogUtil.i(e.toString());
		}
		
	}

	
	private void ShowOOMToast() 
	{
		LogUtil.e("ShowOOMToast");
		Toast.makeText(this,getString(R.string.out_of_memory), Toast.LENGTH_SHORT).show();
	}

//	private void Effective(LOMOType lt) {
//
//		// 首先获取图片像素数据
//		int width = mBitmapLst.get(miBeautyPos).getWidth();
//		int height = mBitmapLst.get(miBeautyPos).getHeight();
//		if (null == pixels || pixels.length != width * height) {
//			pixels = new int[width * height];
//			inBf = new short[pixels.length];
//			outBf = new short[inBf.length];
//		}
//
//		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width,
//				height);
//		RGB_8888_To_RGB_565(pixels, inBf);
//
//		SpecialEffect se = new SpecialEffect();
//
//		try {
//			switch (lt) {
//			case FILLLIGHT:
//				se.FillLight(inBf, inBf.length, outBf, outBf.length, width,
//						height, 125);
//				break;
//			case CHANGESTUDIO_BLUE:
//				se.ChangeStudioExt(inBf, inBf.length, outBf, outBf.length,
//						width, height, E_STUDIO_TYPE.uuphoto_editor_cold_blue);
//				break;
//			case CHANGESTUDIO_GREEN:
//				se.ChangeStudioExt(inBf, inBf.length, outBf, outBf.length,
//						width, height, E_STUDIO_TYPE.uuphoto_editor_cold_green);
//				break;
//			case CHANGEHUE:
//				se.ChangeHue(inBf, inBf.length, outBf, outBf.length, width,
//						height, 50);
//				break;
//			case SOFTGLOW:
//				se.SoftGlow(inBf, inBf.length, outBf, outBf.length, width,
//						height, 8, 75);
//				break;
//			case CHANGESTUDIO_YELLOW:
//				se
//						.ChangeStudioExt(inBf, inBf.length, outBf,
//								outBf.length, width, height,
//								E_STUDIO_TYPE.uuphoto_editor_warm_yellow);
//				break;
//			case CHANGESTUDIO_GRAY: {
//				ImageFilter image = new ImageFilter(pixels, width, height);
//				Bitmap tempBmp = mTempBmp;
//				mTempBmp = image.grayFilter();
//				mPhotoIV.setImageBitmap(mTempBmp);
//				if (tempBmp != null && !tempBmp.isRecycled())
//					tempBmp.recycle();
//				tempBmp = null;
//				return;
//			}
//			case CHANGESTUDIO_CHANGEGREY: {
//				ImageFilter image = new ImageFilter(pixels, width, height);
//				Bitmap tempBmp = mTempBmp;
//				mTempBmp = image.changeGrey();
//				mPhotoIV.setImageBitmap(mTempBmp);
//				if (tempBmp != null && !tempBmp.isRecycled())
//					tempBmp.recycle();
//				tempBmp = null;
//				return;
//			}
//
//			case CHANGESTUDIO_RETRO:
//				se.ChangeStudioExt(inBf, inBf.length, outBf, outBf.length,
//						width, height, E_STUDIO_TYPE.uuphoto_editor_retro);
//				break;
//			case CHANGESTUDIO_FILM:
//				se.ChangeStudioExt(inBf, inBf.length, outBf, outBf.length,
//						width, height, E_STUDIO_TYPE.uuphoto_editor_film);
//				break;
//			// case CHANGESTUDIO_TOTAL:
//			// se.ChangeStudioExt(inBf, inBf.length, outBf, outBf.length, width,
//			// height,E_STUDIO_TYPE.uuphoto_editor_studio_total);
//			// break;
//			default:
//				return;
//
//			}
//			RGB_565_To_RGB_8888(inBf, pixels);
//			Bitmap tempBmp = mTempBmp;
//			mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width,
//					width, height, Bitmap.Config.RGB_565);
//			mPhotoIV.setImageBitmap(mTempBmp);
//			if (tempBmp != null && !tempBmp.isRecycled())
//				tempBmp.recycle();
//			tempBmp = null;
//		}
//
//		catch (Exception e) {
//			LogUtil.i(e.toString());
//		}
//
//	}

	@Override
	public void onLowMemory() {
		LogUtil.e("EffectsProcessingActivity onLowMemory");
		super.onLowMemory();
	}

	@Override
	public void colorChanged(int color) {
		mScrawlView.getPaint().setColor(color);

	}

	// private static final int COLOR_MENU_ID = Menu.FIRST;
	// private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
	// private static final int BLUR_MENU_ID = Menu.FIRST + 2;
	// private static final int ERASE_MENU_ID = Menu.FIRST + 3;
	// private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
	public void OnScrawItemSelect(int position) {
		mScrawlView.getPaint().setXfermode(null);
		mScrawlView.getPaint().setAlpha(0xFF);

		switch (/* item.getItemId() */position) {
		case /* COLOR_MENU_ID */1:
			new ColorPickerDialog(this, this, mScrawlView.getPaint().getColor())
					.show();
			return;
		case /* EMBOSS_MENU_ID */2:
			if (mScrawlView.getPaint().getMaskFilter() != mScrawlView
					.getEmboss()) {
				mScrawlView.getPaint().setMaskFilter(mScrawlView.getEmboss());
			} else {
				mScrawlView.getPaint().setMaskFilter(null);
			}
			return;
		case /* BLUR_MENU_ID */3:
			if (mScrawlView.getPaint().getMaskFilter() != mScrawlView.getBlur()) {
				mScrawlView.getPaint().setMaskFilter(mScrawlView.getBlur());
			} else {
				mScrawlView.getPaint().setMaskFilter(null);
			}
			return;
		case /* ERASE_MENU_ID */4:
			mScrawlView.getPaint().setXfermode(
					new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
			return;
		case /* SRCATOP_MENU_ID */5:
			mScrawlView.getPaint().setXfermode(
					new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
			mScrawlView.getPaint().setAlpha(0x80);
			return;
		}
	}

	private static final int THEMENOTIFY = 0;

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case THEMENOTIFY:
			String disagreeTermMsg = "getString(R.string.theme_download_notify)";
			return new AlertDialog.Builder(this).setIcon(
					/*R.drawable.dialog_icon_notify*/100).setTitle(
					"getString(R.string.new_theme_title)").setMessage(
					disagreeTermMsg).setNegativeButton(
					"getString(R.string.cancel)",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

						}

					}).setMessage(disagreeTermMsg).setPositiveButton(
					"getString(R.string.ok)",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							OnDownloadTheme();
						}

					}).create();
		}
		return null;
	}

	protected void OnDownloadTheme() {

		Intent intent = new Intent();
		//intent.setClass(this, EffectsDownloadActivity.class);
		intent.putExtra(Globals.EXTRA_TYPE, -1);
		startActivity(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case REQUESTCODE_PUBLISH:
			if (Activity.RESULT_OK == resultCode) {
				finish();
			}

			break;
		default:
			break;
		}

	}
	
	public void Old() {
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("framecombo/lzp_b.jpg",
				localAssetManager);
		if(null == localBitmap1)
		{
			mbOutofMem = true;
			return;
		}
		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = BitmapUtil.instance()
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		if(null == localBitmap2)
		{
			if(null != localBitmap1 && !localBitmap1.isRecycled())
			{
				localBitmap1.recycle();
			}
			mbOutofMem = true;
			return;
		}
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		if(null == pixels2 || pixels2.length != localBitmap2.getWidth()
				* localBitmap2.getHeight())
		{
			pixels2 = new int[localBitmap2.getWidth()
			  				* localBitmap2.getHeight()];			
		}
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleOldPhoto(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
		if(mTempBmp.isMutable())
			mTempBmp.setPixels(pixels, 0, width, 0, 0, width, height);
		else
		{
			mTempRecycled = mTempBmp;
			mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
		}
		if(null != localBitmap1 && !localBitmap1.isRecycled())
		{
			localBitmap1.recycle();
		}
		if(null != localBitmap2 && !localBitmap2.isRecycled())
		{
			localBitmap2.recycle();
		}
		//im.setImageBitmap(map);
	}

	public void Lomo() {
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("framecombo/2.jpg",
				localAssetManager);
		if(null == localBitmap1)
		{
			mbOutofMem = true;
			return;
		}
		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = BitmapUtil.instance()
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		if(null == localBitmap2)
		{
			if(null != localBitmap1 && !localBitmap1.isRecycled())
			{
				localBitmap1.recycle();
			}
			mbOutofMem = true;
			return;
		}
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		if(null == pixels2 || pixels2.length != localBitmap2.getWidth()
				* localBitmap2.getHeight())
		{
			pixels2 = new int[localBitmap2.getWidth()
			  				* localBitmap2.getHeight()];			
		}
		
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoSnow(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
		//Bitmap map = Bitmap
		//		.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		if(mTempBmp.isMutable())
			mTempBmp.setPixels(pixels, 0, width, 0, 0, width, height);
		else
		{
			mTempRecycled = mTempBmp;
			mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
		}
		if(null != localBitmap1 && !localBitmap1.isRecycled())
		{
			localBitmap1.recycle();
		}
		if(null != localBitmap2 && !localBitmap2.isRecycled())
		{
			localBitmap2.recycle();
		}
	}
	public void LomoHaolo() {
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("framecombo/qcgy.jpg",
				localAssetManager);
		if(null == localBitmap1)
		{
			mbOutofMem = true;
			return;
		}
		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = BitmapUtil.instance()
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		if(null == localBitmap2)
		{
			if(null != localBitmap1 && !localBitmap1.isRecycled())
			{
				localBitmap1.recycle();
			}
			mbOutofMem = true;
			return;
		}
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		if(null == pixels2 || pixels2.length != localBitmap2.getWidth()
				* localBitmap2.getHeight())
		{
			pixels2 = new int[localBitmap2.getWidth()
			  				* localBitmap2.getHeight()];			
		}
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoHalo(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
//		Bitmap map = Bitmap
//				.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		
		if(mTempBmp.isMutable())
			mTempBmp.setPixels(pixels, 0, width, 0, 0, width, height);
		else
		{
			mTempRecycled = mTempBmp;
			mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
		}
			
		if(null != localBitmap1 && !localBitmap1.isRecycled())
		{
			localBitmap1.recycle();
		}
		if(null != localBitmap2 && !localBitmap2.isRecycled())
		{
			localBitmap2.recycle();
		}
	}
	
	public void Sharp(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.Sharp(pixels, width, height,2.0f,1.0f);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
//	public void BackWeakRadius(){
//		int width = bmp.getWidth();
//		int height = bmp.getHeight();
//		int[] pixels = new int[width * height];
//		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
//		jni.SetBackWeakRadius(pixels, width, height);
//		Bitmap map = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
//		im.setImageBitmap(map);
//	}
	
	public void Reverse() {
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		AssetManager localAssetManager = getBaseContext().getAssets();
		Bitmap localBitmap1 = AssertFile("framecombo/fzs.png",
				localAssetManager);
		if(null == localBitmap1)
		{
			mbOutofMem = true;
			return;
		}
		// 计算缩放率，新尺寸除原始尺寸
		float scaleWidth = ((float) width) / localBitmap1.getWidth();
		float scaleHeight = ((float) height) / localBitmap1.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();

		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);

		// 创建新的图片
		Bitmap localBitmap2 = BitmapUtil.instance()
				.createBitmap(localBitmap1, 0, 0, localBitmap1.getWidth(),
						localBitmap1.getHeight(), matrix, true);

		if(null == localBitmap2)
		{
			if(null != localBitmap1 && !localBitmap1.isRecycled())
			{
				localBitmap1.recycle();
			}
			mbOutofMem = true;
			return;
		}
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		if(null == pixels2 || pixels2.length != localBitmap2.getWidth()
				* localBitmap2.getHeight())
		{
			pixels2 = new int[localBitmap2.getWidth()
			  				* localBitmap2.getHeight()];			
		}
		localBitmap2.getPixels(pixels2, 0, localBitmap2.getWidth(), 0, 0,
				localBitmap2.getWidth(), localBitmap2.getHeight());
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleReverse(pixels, pixels2, localBitmap2.getWidth(), height);

		Log.d("ImageProcess", width + "   " + height);
		//Bitmap map = Bitmap
		//		.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		if(mTempBmp.isMutable())
			mTempBmp.setPixels(pixels, 0, width, 0, 0, width, height);
		else
		{
			mTempRecycled = mTempBmp;
			mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
		}	
		if(null != localBitmap1 && !localBitmap1.isRecycled())
		{
			localBitmap1.recycle();
		}
		if(null != localBitmap2 && !localBitmap2.isRecycled())
		{
			localBitmap2.recycle();
		}
	}
	
//	public void BackWeakRadius(){
//		int width = bmp.getWidth();
//		int height = bmp.getHeight();
//		int[] pixels = new int[width * height];
//		bmp.getPixels(pixels, 0, width, 0, 0, width, height);
//		jni.SetBackWeakRadius(pixels, width, height);
//		Bitmap map = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
//		im.setImageBitmap(map);
//	}
	
	public void LomoC(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoC(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
		//im.setImageBitmap(map);
	}
	
	public void Cinnamon(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleCinnamon(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void Elegant(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleElegant(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void Film(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleFilm(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void Impression(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleImpression(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void Japanese(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleJapanese(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void Retro(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleRetro(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void XiaoQinxin(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleXiaoQingXin(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void LomoHDR(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoHDR(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void BaoColor(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleBaoColor(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	
	public void LomoB(){
		int width = mBitmapLst.get(miBeautyPos).getWidth();
		int height = mBitmapLst.get(miBeautyPos).getHeight();
		if(null == pixels || pixels.length != width * height)
		{
			pixels = new int[width * height];			
		}
		mBitmapLst.get(miBeautyPos).getPixels(pixels, 0, width, 0, 0, width, height);
		jni.StyleLomoB(pixels, width, height);
		mTempRecycled = mTempBmp;
		mTempBmp = BitmapUtil.instance().createBitmap(pixels, 0, width, width, height, Bitmap.Config.ARGB_8888);
	}
	public Bitmap AssertFile(String paramString, AssetManager paramAssetManager) {
		InputStream localInputStream;
		Bitmap localObject3 = null;
		try {

			new StringBuilder("LoadAssertsPic path=").append(paramString)
					.toString();
			localInputStream = paramAssetManager.open(paramString);
			localObject3 = BitmapUtil.instance().DecodeFromStream(localInputStream, null,
					new BitmapFactory.Options());
		} catch (IOException localIOException2) {

		}
		return localObject3;
	}
	private static JNI jni = new JNI();
}